﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VOL;
using BLL;

namespace Web.Admin
{
    public partial class NhaCungCapForm : System.Web.UI.Page
    {
        NhaCungCapBL _NhaCungCapBLL = new NhaCungCapBL();
        int MaNhaCungCap = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string _MaNhaCungCap = Request.QueryString["MaNhaCungCap"];

                if (_MaNhaCungCap == null || _MaNhaCungCap == "" || !int.TryParse(_MaNhaCungCap, out MaNhaCungCap))
                {
                    hdfMaNhaCungCap.Value = "0";
                }
                else
                {
                    DataTable dt = _NhaCungCapBLL.searchIDNhaCungCap(MaNhaCungCap);

                    txtDienThoai.Text = dt.Rows[0]["DienThoai"].ToString();
                    txtDiaChi.Text = dt.Rows[0]["DiaChi"].ToString();
                    txtEmail.Text = dt.Rows[0]["Email"].ToString();
                    txtTenNhaCungCap.Text = dt.Rows[0]["TenNhaCungCap"].ToString();
                    hdfMaNhaCungCap.Value = _MaNhaCungCap;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string _MaNhaCungCap = hdfMaNhaCungCap.Value;
            if (_MaNhaCungCap == null || _MaNhaCungCap == "0")
            {
                // thực hiện thêm mới
                NhaCungCap obj = new NhaCungCap();
                obj.DiaChi = txtDiaChi.Text;
                obj.DienThoai = txtDienThoai.Text;
                obj.Email = txtEmail.Text;
                obj.TenNhaCungCap = txtTenNhaCungCap.Text;
                _NhaCungCapBLL.insertNhaCungCap(obj);
            }
            else
            {
                // thực hiện sửa
                NhaCungCap obj = new NhaCungCap();
                obj.DiaChi = txtDiaChi.Text;
                obj.DienThoai = txtDienThoai.Text;
                obj.Email = txtEmail.Text;
                obj.TenNhaCungCap = txtTenNhaCungCap.Text;
                obj.MaNhaCungCap = int.Parse(_MaNhaCungCap);
                _NhaCungCapBLL.updateNhaCungCap(obj);
            }

            Response.Redirect("~/Admin/NhaCungCapManager.aspx");
        }
    }
}